package zhihui.mobile.manager.user;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zhihui.mobile.bean.user.UserInfo;
import zhihui.mobile.config.Config;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.AllManagerObvs;
import zhihui.mobile.manager.IManager;
import zhihui.mobile.net.HttpUtils;

/**
 * Created by 张公铭 on 2017/7/3 0003.
 * 用户信息管理类
 */

public class UserManager implements IManager
{
    private Logger logger = Logger.getLogger(UserManager.class);
    private static UserManager manager;
    private UserInfo curUser;       //当前用户信息

    private UserManager()
    {
        AllManagerObvs.getInstance().addManager(this);
    }

    private synchronized static void createdIns()
    {
        if (manager == null)
        {
            manager = new UserManager();
        }
    }

    public static UserManager getInstance()
    {
        if (manager == null)
        {
            createdIns();
        }
        return manager;
    }

    public UserInfo getCurUser()
    {
        return curUser;
    }

    public void setCurUser(UserInfo curUser)
    {
        this.curUser = curUser;
    }


    public void uploadPhoto(IHttpCallBackListener listener, String file)
    {
        HttpUtils.postFile("http://39.108.190.27/html/upload", curUser.getId().toString(), file, listener);
    }

    public void updateUserInfo(IHttpCallBackListener listener, String name, int sex)
    {
        Map<String, String> params = new HashMap<>();
        params.put("service", "user");
        params.put("method", "updateMsg");

        List<Object> list = new ArrayList<>();
        list.add(curUser.getId());
        list.add("");
        list.add(name);
        list.add(sex);

        HttpUtils.postAes(Config.URL, params, list, listener);
    }

    @Override
    public void clear()
    {
        AllManagerObvs.getInstance().removeManager(this);
    }
}
